<?php
/**
 * 测试 IMEI 检查逻辑
 */

// 定义应用根目录
define('APP_PATH', __DIR__ . '/application/');
define('RUNTIME_PATH', __DIR__ . '/runtime/');

require __DIR__ . '/thinkphp/base.php';

use think\Db;
use app\service\MqttMessageHandler;

echo "========== IMEI 检查测试 ==========\n\n";

$imei = '861179035726231';

echo "【测试1】直接查询数据库\n";
try {
    $count = Db::name('flow_wifi_imei')->where('IMEI', $imei)->count();
    echo "查询结果: " . $count . " 条记录\n";
    echo "IMEI 存在: " . ($count > 0 ? "是" : "否") . "\n";
} catch (\Throwable $e) {
    echo "❌ 查询失败: " . $e->getMessage() . "\n";
}

echo "\n【测试2】使用 MqttMessageHandler 的 batchCheckImeiExists()\n";
try {
    $handler = new MqttMessageHandler();
    $result = $handler->batchCheckImeiExists([$imei]);
    echo "检查结果: " . json_encode($result) . "\n";
    echo "IMEI 存在: " . (in_array($imei, $result) ? "是" : "否") . "\n";
} catch (\Throwable $e) {
    echo "❌ 检查失败: " . $e->getMessage() . "\n";
}

echo "\n【测试3】使用 MqttMessageHandler 的 forceCheckImeiExists()\n";
try {
    $handler = new MqttMessageHandler();
    $result = $handler->forceCheckImeiExists($imei);
    echo "检查结果: " . ($result ? "存在" : "不存在") . "\n";
} catch (\Throwable $e) {
    echo "❌ 检查失败: " . $e->getMessage() . "\n";
}

echo "\n【测试4】检查缓存\n";
try {
    $cacheKey = 'imei_exists:' . $imei;
    $cached = \think\facade\Cache::get($cacheKey);
    echo "缓存值: " . json_encode($cached) . "\n";
} catch (\Throwable $e) {
    echo "缓存不可用: " . $e->getMessage() . "\n";
}

echo "\n========== 测试完成 ==========\n";

